﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EZBarTender_Wrapper.SmallLabels
{
	public partial class ChangeByUpc : Form
	{
		private string _connectionString;

		public ChangeByUpc(string connectionString)
		{
			InitializeComponent();
			_connectionString = connectionString;
		}

		private void textBoxUpc_Validated(object sender, EventArgs e)
		{
			while (textBoxUpc.Text.Length < 13)
				textBoxUpc.Text = "0" + textBoxUpc.Text;
		}

		private void textBoxUpc_TextChanged(object sender, EventArgs e)
		{
			if (textBoxUpc.Text.Length == 13)
			{
				EnableButtons(true);

				Authenticate();
			}
			else
				EnableButtons(false);
		}

		private void EnableButtons(bool enable)
		{
			buttonApply.Enabled = enable;
		}

		private void buttonApply_Click(object sender, EventArgs e)
		{
			string upc = textBoxUpc.Text;
			string flag = checkBoxSmallItem.Checked ? "SM" : null;

			SmallLabelDataAccess.ChangeSmallLabel(_connectionString, upc, flag);

			textBoxUpc.Text = "";
			checkBoxSmallItem.Checked = false;
			textBoxUpc.Focus();
		}

		private void Authenticate()
		{
			checkBoxSmallItem.Checked
				= SmallLabelDataAccess.GetSmallLabelStatus(_connectionString, textBoxUpc.Text) == "SM";
		}

		private void textBoxUpc_KeyPress(object sender, KeyPressEventArgs e)
		{

			if (e.KeyChar == 13) // Enter
			{
				Authenticate();
				checkBoxSmallItem.Focus();
			}
		}
	}
}
